// pages/task/task.js
var comm = require('../module.js')
var time = require('../../utils/util.js')
var lqrw_id = 0; //领取任务
var zdr = 0; //是否 指定人任务  0 否 1 是
var data = []
var zd_data = []
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    zdrw_status:1,
    rw_status:1,
    isHidden: true,
    interVal: 2000,
    rw_url: "",
    imgUrls: [
      // "../images/1_02.gif", "../images/1_02.gif", "../images/1_02.gif"
    ],
    content: [],
    startX: 0, //开始坐标

    startY: 0,
    zd_content: []
  },
  // 取消
  cancel: function () {
    this.setData({
      isHidden: true
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      // console.log(options)
    // console.log(time.formatTime(1540285555,"Y-M-D h:m:s"));
   
  },
  //手指触摸动作开始 记录起点X坐标

  touchstart: function (e) {

    //开始触摸时 重置所有删除

    this.data.content.forEach(function (v, i) {

      if (v.isTouchMove) //只操作为true的

        v.isTouchMove = false;

    })

    this.data.zd_content.forEach(function (v, i) {

      if (v.isTouchMove) //只操作为true的

        v.isTouchMove = false;

    })

    this.setData({

      startX: e.changedTouches[0].clientX,

      startY: e.changedTouches[0].clientY,

      content: this.data.content

    })

    this.setData({

      startX: e.changedTouches[0].clientX,

      startY: e.changedTouches[0].clientY,

      zd_content: this.data.zd_content

    })

  },
  //手指触摸动作开始 记录起点X坐标

  touchstart1: function (e) {

    //开始触摸时 重置所有删除

    this.data.zd_content.forEach(function (v, i) {

      if (v.isTouchMove) //只操作为true的

        v.isTouchMove = false;

    })

    this.data.content.forEach(function (v, i) {
      // console.log(v)
      if (v.isTouchMove) {
        v.isTouchMove = false;
      } //只操作为true的



    })

    this.setData({

      startX: e.changedTouches[0].clientX,

      startY: e.changedTouches[0].clientY,

      zd_content: this.data.zd_content

    })

    this.setData({

      startX: e.changedTouches[0].clientX,

      startY: e.changedTouches[0].clientY,

      content: this.data.content

    })
  },

  //滑动事件处理

  touchmove: function (e) {

    var that = this,

      index = e.currentTarget.dataset.index, //当前索引

      startX = that.data.startX, //开始X坐标

      startY = that.data.startY, //开始Y坐标

      touchMoveX = e.changedTouches[0].clientX, //滑动变化坐标

      touchMoveY = e.changedTouches[0].clientY, //滑动变化坐标

      //获取滑动角度

      angle = that.angle({
        X: startX,
        Y: startY
      }, {
          X: touchMoveX,
          Y: touchMoveY
        });

    that.data.content.forEach(function (v, i) {

      v.isTouchMove = false

      //滑动超过30度角 return

      if (Math.abs(angle) > 30) return;

      if (i == index) {

        if (touchMoveX > startX) //右滑

          v.isTouchMove = false

        else //左滑

          v.isTouchMove = true

      }

    })

    //更新数据

    that.setData({

      content: that.data.content

    })

  },

  //滑动事件处理

  touchmove1: function (e) {

    var that = this,

      index = e.currentTarget.dataset.index, //当前索引

      startX = that.data.startX, //开始X坐标

      startY = that.data.startY, //开始Y坐标

      touchMoveX = e.changedTouches[0].clientX, //滑动变化坐标

      touchMoveY = e.changedTouches[0].clientY, //滑动变化坐标

      //获取滑动角度

      angle = that.angle({
        X: startX,
        Y: startY
      }, {
          X: touchMoveX,
          Y: touchMoveY
        });

    that.data.zd_content.forEach(function (v, i) {

      v.isTouchMove = false

      //滑动超过30度角 return

      if (Math.abs(angle) > 30) return;

      if (i == index) {

        if (touchMoveX > startX) //右滑

          v.isTouchMove = false

        else //左滑

          v.isTouchMove = true

      }

    })

    //更新数据

    that.setData({

      zd_content: that.data.zd_content

    })

  },

  /**
  
  * 计算滑动角度
  
  * @param {Object} start 起点坐标
  
  * @param {Object} end 终点坐标
  
  */

  angle: function (start, end) {

    var _X = end.X - start.X,

      _Y = end.Y - start.Y

    //返回角度 /Math.atan()返回数字的反正切值

    return 360 * Math.atan(_Y / _X) / (2 * Math.PI);

  },

  //领取任务

  del: function (e) {
    var data = "ip=" + wx.getStorageSync("ph") + "&a_id=" + e.currentTarget.dataset.taskid
    console.log(e)
    comm.ajax("Xcxflower/getTask?" + data, "", res => {
      // console.log(res)
      // var _this = this
      if (res.data.status != 10000) {
        var aid = e.currentTarget.dataset.taskid.toString()
        wx.setStorageSync(e.currentTarget.dataset.taskid.toString(), aid)
        comm.alert(res.data.message)



        this.data.content.forEach(function (v, k) {
          console.log(v.article_id)
          wx.getStorageInfoSync().keys.forEach(function (zd_v, kk) {
            if (zd_v.includes(v.article_id)) {
              v.lq_txt = 1
              console.log(v)
            }
          })

        })

        this.data.zd_content.forEach(function (vv, kk) {
          // console.log(vv)
          wx.getStorageInfoSync().keys.forEach(function (w_v, kk) {
            if (w_v.includes(vv.article_id)) {
              vv.lq_txt = 1
              console.log(vv)
            }
          })
          console.log(vv)
        })

        console.log(this.data.content)

      } else {
        console.log(res)
        this.setData({
          isHidden: false
        })
        lqrw_id = e.currentTarget.dataset.taskid
        zdr = e.currentTarget.dataset.zd
        console.log(lqrw_id)
      }
    }, "GET")

  },
  //提交领取任务
  tjrw: function (e) {
    if (!comm.empty(e.detail.value.wx_url, "公众号链接不能为空")) {
      return false;
    }
    comm.ajax("Xcxflower/getTask", {
      ip: wx.getStorageSync("ph"),
      a_id: lqrw_id,
      zhidingren: zdr,
      url: e.detail.value.wx_url
    }, res => {
      console.log(res)
      // var _this = this
      if (res.data.status != 10000) {
        comm.alert(res.data.message)
      } else {
        // console.log(this.data.content)
        comm.alert(res.data.message)
        this.setData({
          isHidden: true,
          rw_url: ""
        })
        var aid = lqrw_id.toString()
        // console.log(aid)
        wx.setStorageSync(lqrw_id.toString(), aid)

        // console.log(...data)
        if (zdr == 0) {
          this.data.content.forEach(function (v, k) {
            console.log(v.article_id)
            console.log(wx.getStorageInfoSync())
            console.log(...wx.getStorageInfoSync().keys)
            wx.getStorageInfoSync().keys.forEach(function (w_v, kk) {
              if (w_v.includes(v.article_id)) {
                v.lq_txt = 1
                console.log(w_v)
              }
            })

          })
          this.setData({
            content: this.data.content
          })
        }
        else {
          this.data.zd_content.forEach(function (v, k) {
            console.log(wx.getStorageInfoSync().keys)
            console.log(...wx.getStorageInfoSync().keys)
            wx.getStorageInfoSync().keys.forEach(function (vv, kk) {
              if (vv.includes(v.article_id)) {
                console.log(vv)
                v.lq_txt = 1
              }
            })
          })
          this.setData({
            zd_content: this.data.zd_content
          })
        }

      }
    }, "POST")
  },
 
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    console.log("Sfd")
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      content: [],
      zd_content: []
    })
    // console.log("1")
    var data = wx.getStorageSync("ph")
    comm.ajax("Xcxflower/getArticle?ip=" + data, "", res => {
      console.log(res)
      var _this = this
      if (res.data.status != 10000) {
        // comm.alert(res.data.message)
        this.setData({
          rw_status: 0
        })
      } else {

        // console.log(res.data.data)
        let len = res.data.data.length;
        // console.log(len)

        for (let i = 0; i < len; i++) {
          res.data.data[i].create_article_time = time.formatTime(res.data.data[i].create_article_time, "Y-M-D h:m:s")
          res.data.data[i].zd = 0
          // console.log(wx.getStorageInfoSync())
          wx.getStorageInfoSync().keys.forEach(function (vv, kk) {
            if (vv.includes(res.data.data[i].article_id)) {
              // console.log("sd");
              res.data.data[i].lq_txt = 1
            }
          })
        }

        this.setData({
          content: this.data.content.concat(res.data.data)
        })
        // console.log(this.data.content)
      }
    }, "GET")

    //lunbo
    comm.ajax("Xcxflower/getXcxLunbotu", "", res => {
      // console.log(res)
      this.setData({
        imgUrls: res.data.data
      })

    }, "GET")

    // 指定任务
    var data = wx.getStorageSync("ph")
    comm.ajax("Xcxflower/getZhiding?ip=" + data, "", res => {
      console.log(res)
      var _this = this
      if (res.data.status != 10000) {
        // comm.alert("指定任务"+res.data.message)

        this.setData({
          zdrw_status: 0
        })
      } else {
        let len = res.data.data.length;

        for (let i = 0; i < len; i++) {
          res.data.data[i].create_article_time = time.formatTime(res.data.data[i].create_article_time, "Y-M-D h:m:s")
          res.data.data[i].zd = 1
          console.log(wx.getStorageInfoSync().keys)
          wx.getStorageInfoSync().keys.forEach(function (vv, kk) {
            if (vv.includes(res.data.data[i].article_id)) {
              // console.log("sd");
              res.data.data[i].lq_txt = 1
            }
          })

        }

        this.setData({
          zd_content: this.data.zd_content.concat(res.data.data)
        })
        // console.log(this.data.content)
      }
    }, "GET")
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})